<template>
  <transition name="list-fade">
    <div class="playlist" v-show="showFlag" @click="hide">
      <div class="list-wrapper" @click.stop>
        <div class="list-header">
          <h1 class="title">
            <i :class="modeIcon" class="iconfont" @click.stop="changeMode"></i>
            <span class="text">{{modeText}}</span>
            <span class="clear" @click.stop="showConfrim">
              <i class="iconfont icon-clear2"></i>
            </span>
          </h1>
        </div>
        <SCroll class="list-content" :data="sequenceList" ref="listContent" :delayTime="delayTime">
          <transition-group name="list" tag="ul">
            <li
              class="item"
              ref="listLi"
              v-for="(item,index) in sequenceList"
              @click.stop="selectItem(item,index)"
              :key="item.id"
            >
              <i class="current" :class="grtCurrentIcon(item)"></i>
              <span class="text">{{item.name}}</span>
              <span class="like" @click.stop="toggleFavorite(item)">
                <i :class="favoriteIcon(item)"></i>
              </span>
              <span class="delete" @click.stop="deleteOne(item)">
                <i class="iconfont icon-closesepwindow"></i>
              </span>
            </li>
          </transition-group>
        </SCroll>
        <div class="list-operate">
          <div class="add">
            <i class="add" @click="addTolist">
              <i class="iconfont icon-add"></i>
              <span class="text">添加歌曲到队列</span>
            </i>
          </div>
        </div>
        <div class="list-close" @click.stop="hide">
          <span>关闭</span>
        </div>
      </div>
      <!-- 弹窗 -->
      <confirm text="是否清空当前的播放列表" ref="confirm" @confirm="deleteAll"></confirm>
      <!-- 弹窗 -->
      <add-song ref="addSong"></add-song>
    </div>
  </transition>
</template>
<script>
import { mapMutations, mapGetters, mapActions } from "vuex";
import SCroll from "@/base/scroll/scroll";
import { playMode } from "@/common/js/config";
import confirm from "@/base/confirm/confirm";
import { playerMixin } from "@/common/js/mixin";
import addSong from "@/components/add-song/add-song"
export default {
  mixins: [playerMixin],
  data() {
    return {
      showFlag: false, //控制列表的显示与隐藏
      delayTime: 100,//scroll的刷新时间
    };
  },
  computed: {
    modeText(){
      return this.mode === playMode.random ? '随机播放': this.mode === playMode.loop ? '单曲循环播放':'顺序播放'
    }
  },
  components: {
    SCroll,
    confirm,
    addSong
  },
  methods: {
    show() {
      this.showFlag = true;

      setTimeout(() => {
        this.$refs.listContent.refresh();
        this.scrollCurrent(this.currentSong);
      }, 20);
    },
    hide() {
      this.showFlag = false;
    },
    //计算当前播放歌曲的样式
    grtCurrentIcon(item) {
      if (item.id === this.currentSong.id) {
        return "iconfont icon-play";
      } else {
        return "";
      }
    },
    // 点击播放当前点击的歌曲
    selectItem(item, index) {
      // 判断当前的播放模式是不是随机播放模式
      if (playMode.random === this.mode) {
        index = this.playlist.findIndex(song => {
          return song.id === item.id;
        });
      }
      this.set_current_index(index);
      if (!this.playing) {
        this.changePlaying(true);
      }
    },
    // 获取当前歌曲的位置
    scrollCurrent(current) {
      const index = this.sequenceList.findIndex(song => {
        return current.id === song.id;
      });
      this.$refs.listContent.scrollToElement(this.$refs.listLi[index], 300);
    },
    // 删除某一项位置
    deleteOne(item) {
      this.deleteListOne(item);
      if (!this.playlist.length) {
        this.hide();
      }
    },
    // 点击删除整个播放列表
    showConfrim() {
      this.$refs.confirm.show();
    },
    deleteAll() {
      this.deleteAllPlaylist();
      this.hide();
    },
    // 点击添加到歌曲列表
    addTolist(){
      this.$refs.addSong.show()
    },
    ...mapActions(["deleteListOne", "deleteAllPlaylist"])
  },
  watch: {
    currentSong(newSong, oldSong) {
      if (!this.showFlag || newSong.id === oldSong.id) {
        return;
      }
      this.scrollCurrent(newSong);
    }
  }
};
</script>
<style lang="stylus">
@import '../../common/stylus/variable';
@import '../../common/stylus/mixin';

.playlist {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 200;
  box-sizing: border-box;
  background-color: $color-background-d;

  &.list-fade-enter-active, &.list-fade-leave-active {
    transition: opacity 0.3s;

    .list-wrapper {
      transition: all 0.3s;
    }
  }

  &.list-fade-enter, &.list-fade-leave-to {
    opacity: 0;

    .list-wrapper {
      transform: translate3d(0, 100%, 0);
    }
  }

  &.list-fade-enter, .list-wrapper {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: $color-highlight-background;

    .list-header {
      position: relative;
      padding: 20px 30px 10px 20px;

      .title {
        display: flex;
        align-items: center;

        .iconfont {
          margin-right: 10px;
          font-size: 30px;
          color: $color-theme-d;
        }

        .text {
          flex: 1;
          font-size: $font-size-medium;
          color: $color-text-l;
        }

        .clear {
          extend-click();

          .icon-clear {
            font-size: $font-size-medium;
            color: $color-text-d;
          }
        }
      }
    }

    .list-content {
      max-height: 240px;
      overflow: hidden;

      .item {
        display: flex;
        align-items: center;
        height: 40px;
        padding: 0 30px 0 20px;
        overflow: hidden;

        &.list-enter-active, &.list-leave-active {
          transition: all 0.1s;
        }

        &.list-enter, &.list-leave-to {
          height: 0;
        }

        .current {
          flex: 0 0 20px;
          width: 20px;
          font-size: $font-size-small;
          color: $color-theme-d;
        }

        .iconfont {
          margin-right: 10px;
        }

        .text {
          flex: 1;
          no-wrap();
          font-size: $font-size-medium;
          color: $color-text-d;
        }

        .like {
          extend-click();
          margin-right: 15px;
          font-size: $font-size-small;
          color: $color-theme;

          .icon-favorite {
            color: $color-sub-theme;
          }
        }

        .iconfont {
          font-size: 25px;
        }

        .delete {
          extend-click();
          font-size: $font-size-small;
          color: $color-theme;
        }
      }
    }

    .list-operate {
      width: 48%;
      margin: 20px auto 30px auto;

      .add {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 8px 16px;
        border: 1px solid $color-text-l;
        border-radius: 100px;
        color: $color-text-l;

        .iconfont {
          font-size: 25px;
        }

        .icon-add {
          margin-right: 5px;
          font-size: $font-size-large;
        }

        .text {
          font-size: $font-size-small;
        }
      }
    }

    .list-close {
      text-align: center;
      line-height: 50px;
      background: $color-background;
      font-size: $font-size-medium-x;
      color: $color-text-l;
    }
  }
}
</style>
